package com.commerce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.commerce.dao.CommodityDao;
import com.commerce.dao.UserDao;
import com.commerce.pojo.dto.CommodityDTO;
import com.commerce.pojo.po.Commodity;
import com.commerce.pojo.po.Evaluater;
import com.commerce.pojo.po.User;
import com.commerce.service.CommodityService;
import com.commerce.service.EvaluaterService;
import com.commerce.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.CopyOption;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityServiceImpl  extends ServiceImpl<CommodityDao, Commodity> implements CommodityService {
    @Autowired
    private CommodityDao commodityDao;
    @Autowired
    private UserService userService;
    @Autowired
    private EvaluaterService evaluaterService;

    @Override
    public List selectList(String CommodityName) {
        LambdaQueryWrapper<Commodity> lqw = new LambdaQueryWrapper<Commodity>();
        lqw.like(Strings.isNotEmpty(CommodityName),Commodity::getName,CommodityName);
        List<Commodity> commoditylist = commodityDao.selectList(lqw);
        List<CommodityDTO> commodityDTOlist = new ArrayList<>();
        //查询商品的好评数和评价数
        for(Commodity commodity :commoditylist){
            CommodityDTO commodityDTO = new CommodityDTO();
            BeanUtil.copyProperties(commodity,commodityDTO);
            LambdaQueryWrapper<Evaluater> selectPraise = new LambdaQueryWrapper<Evaluater>();
            selectPraise.eq(Evaluater::getCommodityId,commodity.getId());
            commodityDTO.setCountEvaluate(evaluaterService.count(selectPraise));
            selectPraise.eq(Evaluater::getEvaluate,1);
            commodityDTO.setPraise(evaluaterService.count(selectPraise));
            commodityDTOlist.add(commodityDTO);
        }
        return commodityDTOlist;
    }

    @Override
    public CommodityDTO getOneById(Integer id,Integer UserId) {
        Commodity commodity = commodityDao.selectById(id);
        CommodityDTO commodityDTO = new CommodityDTO();
        BeanUtil.copyProperties(commodity,commodityDTO);
        User seller = userService.getById(commodity.getUserId());
        commodityDTO.setUser(seller);
        LambdaQueryWrapper<Evaluater> lqw = new LambdaQueryWrapper<Evaluater>();
        lqw.eq(id!=null,Evaluater::getCommodityId,id);
        lqw.eq(UserId!=null,Evaluater::getBuyerId,UserId);
        if(evaluaterService.getOne(lqw)!=null)
        commodityDTO.setEvaluationRelationship(evaluaterService.getOne(lqw).getEvaluate());
        return commodityDTO;
    }

    @Override
    public List<Commodity> getByUser(Integer userId) {
        LambdaQueryWrapper<Commodity> lqw = new LambdaQueryWrapper<Commodity>();
        lqw.eq(userId!=null,Commodity::getUserId,userId);
        return commodityDao.selectList(lqw);
    }
}
